<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>百度</h1>
    <form action="http://localhost:3000/register" method="get">
        用户名：<input type="text" name="username"> <br>
        <span></span>
        <!-- 密 码：<input type="text" name="password"> <br>
        <span></span>
        <button type="button">注册</button> -->
    </form>
    <script>
        let form = document.querySelector('form');
        let user = form.username;
        let pass = form.password;
        let span = document.querySelector('span');



        // 账号
        user.onchange = function () {
            // console.log(user.value);
            //获取用户输入的内容
            let val = user.value;

            //1.创建ajax
            let ajax = new XMLHttpRequest();
            //2.配置ajax请求信息
            ajax.open('get', `http://localhost:3000/register?username=${val}`);
            //3.发送ajax请求
            ajax.send();
            //4.响应结果
            ajax.onreadystatechange = function () {
                if (ajax.readyState == 4 && ajax.status == 200) {
                    console.log(ajax.responseText);
                    let data = JSON.parse(ajax.responseText);
                    span.innerHTML = data.msg;
                }
            }
        }

    </script>
</body>

</html>